package com.china.springServer.service.impl;

import com.china.springServer.service.KafkaProducer;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Service;
import org.springframework.util.concurrent.ListenableFuture;

import javax.annotation.Resource;

/**
 * @fileName: KafkaProducerImpl
 * @author: LmR
 * @createDate: 2023/10/21 15:14
 * @Desc:
 */
@Slf4j
@Service
public class KafkaProducerImpl implements KafkaProducer {
    private Logger logger = LoggerFactory.getLogger(KafkaProducer.class);
    @Resource
    private KafkaTemplate<String, Object> kafkaTemplate;

    @Override
    public void sendMessage(String message) {
        ListenableFuture<SendResult<String, Object>> future = kafkaTemplate.send("topic-test", 0, "1", message);
        try {
            SendResult<String, Object> sendResult = future.get();
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
    }
}
